package cn.sunlelin.educationserver.controller;

import cn.sunlelin.educationserver.entity.Task;
import cn.sunlelin.educationserver.service.TaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
public class TaskController {
    @Autowired
    private TaskService taskService;

    @PostMapping("/task/createTask")
    public Map<String,Object> createTask(@RequestBody Task task){
        Map<String,Object> map =new HashMap<>();
        if(taskService.createTask(task)){
            map.put("code",200);
            map.put("msg","创建作业成功");
            map.put("dataobject",null);
        }
        else {
            map.put("code",500);
            map.put("msg","创建作业失败");
            map.put("dataobject",null);
        }
        return map;
    }

    @PostMapping("/task/updateTask")
    public Map<String,Object> updateTask(@RequestBody Task task){
        Map<String,Object> map =new HashMap<>();
        if(taskService.updateTask(task)){
            map.put("code",200);
            map.put("msg","修改作业成功");
            map.put("dataobject",null);
        }
        else {
            map.put("code",500);
            map.put("msg","修改作业失败");
            map.put("dataobject",null);
        }
        return map;
    }

    @GetMapping("/task/selectTaskById/{taskId}")
    public Map<String,Object> selectTaskById(@PathVariable Integer taskId){
        Task task=taskService.selectTaskById(taskId);
        Map<String,Object> map =new HashMap<>();
        if(task!=null){
            map.put("code",200);
            map.put("msg","根据Id查询作业成功");
            map.put("dataobject",task);
        }
        else {
            map.put("code",500);
            map.put("msg","根据Id查询作业失败");
            map.put("dataobject",null);
        }
        return map;
    }

    @GetMapping("/user/task/selectTaskByCourseId/{courseId}")
    public Map<String,Object> selectTaskByCourseId(@PathVariable Integer courseId,
                                                   @RequestHeader("userId") Integer userId){
        List<Map<String, Object>> taskList=taskService.selectTaskByCourseId(courseId,userId);
        Map<String,Object> map =new HashMap<>();
        if(taskList!=null){
            map.put("code",200);
            map.put("msg","根据课程Id查询作业成功");
            map.put("dataobject",taskList);
        }
        else {
            map.put("code",500);
            map.put("msg","根据课程Id查询作业失败");
            map.put("dataobject",null);
        }
        return map;
    }

    @GetMapping("/task/selectTaskByChapterId/{chapterId}")
    public Map<String,Object> selectTaskByChapterId(@PathVariable Integer chapterId){
        List<Task> taskList=taskService.selectTaskByChapterId(chapterId);
        Map<String,Object> map =new HashMap<>();
        if(taskList!=null){
            map.put("code",200);
            map.put("msg","根据章节Id查询作业成功");
            map.put("dataobject",taskList);
        }
        else {
            map.put("code",500);
            map.put("msg","根据章节Id查询作业失败");
            map.put("dataobject",null);
        }
        return map;
    }

    @GetMapping("/task/deleteTask/{taskId}")
    public Map<String,Object> deleteTask(@PathVariable Integer taskId){
        Map<String,Object> map =new HashMap<>();
        if(taskService.deleteTask(taskId)){
            map.put("code",200);
            map.put("msg","删除作业成功");
            map.put("dataobject",null);
        }
        else {
            map.put("code",500);
            map.put("msg","删除作业失败");
            map.put("dataobject",null);
        }
        return map;
    }
}
